package org.displayextended
{
	import org.event.BuildCompleteEvent;
	
	/**
	 * AniMovSpriteContainer
	 * 
	 * Wrapper Class to use an AniMovSprite with AniMovSpriteManager
	 * Provides BuildIn/Out hooks as well
	 * 
	 * @author zachary.seabrook
	 */
	public class AniMovSpriteContainer extends AniMovSprite
	{
		/*//////////////////////////////////////////////////////////////////////////
			Properties
		*///////////////////////////////////////////////////////////////////////////
		
		public var overrideChain:Boolean=false;
		
		public var manager:AniMovSpriteManager;
		
		public function get useSequenceIn():Boolean { return manager.useSequenceIn; }
		public function set useSequenceIn( val:Boolean ):void { manager.useSequenceIn = val; }
		
		public function get useSequenceOut():Boolean { return manager.useSequenceOut; }
		public function set useSequenceOut( val:Boolean ):void { manager.useSequenceOut = val; }
		
		/*//////////////////////////////////////////////////////////////////////////
			Public
		*///////////////////////////////////////////////////////////////////////////
		
		/**
		 * Constructor 
		 * @param movieClipClassPathOrInstance
		 */		
		public function AniMovSpriteContainer(movieClipClassPathOrInstance:Object=null)
		{
			super(movieClipClassPathOrInstance);
			manager = new AniMovSpriteManager( this );
			showTrace = false;
		}
		
		/**
		 * Wrapper to Add AniMovSprite to AniMovSpriteManager
		 */	
		public function addToBuildChain( obj:Object ):void
		{
			t( 'addToBuildChain(' + obj + ')' );
			manager.add( obj );
		}
		
		/*//////////////////////////////////////////////////////////////////////////
			Hooks (override these)
		*///////////////////////////////////////////////////////////////////////////
		
		/**
		 * Called immediately BEFORE building in
		 * Place any stage elements/listeners here
		 */	
		protected function addStageElements():void{ /* override */ }
		
		/**
		 * Called immediately AFTER building in, BEFORE the BuildInComplete Event has fired 
		 */	
		protected function onBuildIn():void{ /* override */ }
		
		/**
		 * Called immediately AFTER building in, AFTER the BuildInComplete Event has fired
		 * Add any startup code here
		 */	
		protected function start():void{ /* override */ }
		
		/**
		 * Called immediately BEFORE building OUT
		 * Place any shutdown code here
		 */	
		protected function end():void{ /* override */ }
		
		/**
		 * Called immediately AFTER building OUT, BEFORE the BuildOutComplete Event has fired
		 * Place any Manual Stage removals here
		 */	
		protected function onBuildOut():void{ /* override */ }
		
		protected function onSequenceIn( evt:BuildCompleteEvent ):void
		{
			t( 'onSequenceIn' );
			manager.removeEventListener( BuildCompleteEvent.SEQUENCE_IN_COMPLETE, onSequenceIn );
			super.onBuildInComplete();
		}
		
		protected function onSequenceOut( evt:BuildCompleteEvent ):void
		{
			t( 'onSequenceOut' );
			manager.removeEventListener( BuildCompleteEvent.SEQUENCE_OUT_COMPLETE, onSequenceOut );
			super.buildOut();
		}
		
		/*//////////////////////////////////////////////////////////////////////////
			Overrides
		*///////////////////////////////////////////////////////////////////////////
		
		override public function buildIn():void
		{
			t( 'buildIn' );
			addStageElements();
			super.buildIn();
		}
		
		override protected function onBuildInComplete():void
		{
			t( 'buildInComplete' );
			onBuildIn();
			
			if( manager.sequenceChain.length > 0 && !overrideChain )
			{
				manager.addEventListener( BuildCompleteEvent.SEQUENCE_IN_COMPLETE, onSequenceIn );
				manager.sequenceIn();
			}
			else
			{
				super.onBuildInComplete();
			}
			
			start();
		}
		
		override public function buildOut():void
		{
			t( 'buildOut' );
			end();
			
			if( manager.sequenceChain.length > 0 && !overrideChain )
			{
				manager.addEventListener( BuildCompleteEvent.SEQUENCE_OUT_COMPLETE, onSequenceOut );
				manager.sequenceOut();
			}
			else
			{
				super.buildOut();
			}
		}
		
		override protected function onBuildOutComplete():void
		{
			t( 'onBuildOutComplete' );
			
			onBuildOut();
			super.onBuildOutComplete();
		}
	}
}